<!DOCTYPE html>
<html>
<head lang="zh-cn">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>格悦会</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<style>
  *{
    margin: 0;
    padding: 0;
    list-style: none;
  }
  body,html{
    width: 100%;
    height: 100%;
  }
</style>
</head>  
<body>
    <?php
session_start();
error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));

//微信API初始验证
if($_GET['echostr']){
  check_weixin_api();
  die();
}

//路由跳转
if ($_GET['a']=="getinfo"){
  
  getinfo();die();
  
}elseif($_GET['a']=="getcode"){

  getcode();die();
  
}

else{
  
  getinfo();die();
  
}




function getinfo(){
  
     $appid = 'wx7486af3dbd72e41d';
                
     $appsecret   = '1e493cc067db3cebe5382c2728b0c890';
     
     $redirect = urlencode('http://weixin.job.waijiao.org.cn/index.php?a=getcode');
     
     $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
     
     echo '<meta http-equiv="Refresh" content="0; url='.$url.'" /> '; 
  
}

function getcode(){
  
/*    if(!$_GET['code']){
      
      echo '<meta http-equiv="Refresh" content="0; url=http://3d.yanxishe.cc/index.php">';
        
    } */
  
      $appid = 'wx7486af3dbd72e41d';
                
        $appsecret   = '1e493cc067db3cebe5382c2728b0c890';
        
        $code = $_GET['code'];
        
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        
        $data = array();
        $res = http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));
        
        echo '<meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link href="index.css" rel="stylesheet" type="text/css">
        ';
   
        $resArr = json_decode($res);
        $access_token = $resArr->access_token;
        $openid = $resArr->openid;
        
        
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        
        $res2 =  http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));
        
        $resArr2 = json_decode($res2);
        
        $nickname  = $resArr2->nickname;
        $pic       = $resArr2->headimgurl;
        $sex       = $resArr2->sex;
        $openid    = $resArr2->openid;
        //此处给用户设置session，以便于网页跳转的时候把需要的参数带过去
        $_SESSION['id']=$appid;
        $_SESSION['nickname']=$nickname;
        $_SESSION['pic']=$pic;
        $_SESSION['sex']=$sex;
        $_SESSION['openid']=$openid;

        echo '<meta http-equiv="Refresh" content="0; url=home.php" /> '; 
     echo '
      <div class="main">
          <center>
            <div class="user" >';
              echo "<p>"."<br>";
              echo "<img src='$pic' width='60%'>"."<p>"."<br>"."<p>";
              echo "{$nickname}"."你好，欢迎进入格悦VIP系统";
              echo "<p>"."<br>"."<p>"."<br>"."<p>";
              echo "公告：你已经进入格悦科技！";
              echo '
            </div>
          </center>
        <div class="body" >
            <a href="home.php"><div class="home">首页</div></a>
            <a href="message.php"><div class="message" >我的信息</div></a>
        </div>';
     echo '
      </div>';  
  
}   /* function getcode */   


function check_weixin_api(){

define(TOKEN,'weixin1234');

$timestamp = $_GET['timestamp'];
$nonce     = $_GET['nonce'];
$token     = TOKEN;

$signature = $_GET['signature'];

$tmpArr = array($timestamp,$nonce,$token);

sort($tmpArr,SORT_STRING);

$tmpStr = sha1(implode($tmpArr));

if ($tmpStr == $signature  && $_GET['echostr']){

    echo $_GET['echostr'];

}else{
    //echo '没有Echo Str ，FALSE';
      echo "FALSE";
   
}

}


/**

 * 发送HTTP请求方法

 * @param  string $url    请求URL

 * @param  array  $params 请求参数

 * @param  string $method 请求方法GET/POST

 * @return array  $data   响应数据


 */
function http($url, $params, $method = 'GET', $header = array(), $multi = false){
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件

            $params = $multi ? $params : http_build_query($params);

            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch); 
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;
}


?>

  </div>
  <!-- <a href=""><div class="home" style="background-color:#0c7ab3;float:left;width:49%;height:50px;border:1px solid #000000;font-size:40px;text-align:center;color:#ffffff">首页</div></a><a href=""><div clsaa="message" style="background-color:#0c7ab3;float:right;width:49%;height:50px;border:1px solid #000000;font-size:40px;text-align:center;color:#ffffff">我的信息</div></a> -->
</div>
</body>
</html>
        